package com.eastfair.datasync.comsumer;

import com.alibaba.fastjson.JSONObject;
import com.eastfair.core.datasync.BaseDataSync;
import com.eastfair.core.utils.SpringUtils;
import com.eastfair.core.utils.StringUtils;
import com.eastfair.datasync.model.SyncKeyDefinition;
import com.eastfair.datasync.handler.UserAccountSyncHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Slf4j
//@Component
public class DataSyncComsumer {

    @KafkaListener(topics = "${ef.kafka.datasync}",groupId = "${spring.application.name}")
    public void onMessage(String message) {
        BaseDataSync baseDataSync = JSONObject.parseObject(message, BaseDataSync.class);
        JSONObject jsonObject = (JSONObject) baseDataSync.getSyncObj();
        //执行
        executeFlow(jsonObject.getString("syncName"),baseDataSync);

    }

    /**
     * 执行
     * @param syncName
     * @param baseDataSync
     */
    public void executeFlow(String syncName,BaseDataSync baseDataSync) {
        try{
            if (!StringUtils.isBlank(syncName) && syncName.equals(SyncKeyDefinition.USER_ACCOUNT)) {
                UserAccountSyncHandler userAccountSyncHandler = SpringUtils.getBean(UserAccountSyncHandler.class);
                if(userAccountSyncHandler!=null){
                    //用户账户数据同步
                    userAccountSyncHandler.executeDataSync(baseDataSync.getSyncObj());
                }
            }
        }catch (Exception e){
//            e.printStackTrace();
            //失败执行操作
            log.info("没有实现类，说明该项目不需要同步该数据");
        }
    }
}
